package org.mycub.server;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.webapp.WebAppContext;


public class JettyServer {

	private static String PORTVALUE = "8080";
	
	private Server server;
	private WebAppContext context;
	private HandlerList handlers;
	
	public JettyServer(){
		String port = System.getenv("PORT");
		if(port == null || port.isEmpty()) {
			port = PORTVALUE;
		}
		this.setServer(new Server(Integer.valueOf(port)));
		this.setContext(new WebAppContext());
		
		this.getContext().setDescriptor("./src/main/webapp/WEB-INF/web.xml");
		this.getContext().setResourceBase("./src/main/webapp");
		this.getContext().setContextPath("/");
		this.getContext().setParentLoaderPriority(true);
		
		this.setHandlers(new HandlerList());
		
		this.getHandlers().setHandlers(new Handler[] { this.getContext(), new DefaultHandler() });
		this.getServer().setHandler(handlers);
	}
	
	public void start() throws Exception{
		this.getServer().start();
	}
	
	public void join() throws Exception{
		this.getServer().join();
	}
	public HandlerList getHandlers() {
		return handlers;
	}
	public void setHandlers(HandlerList handlers) {
		this.handlers = handlers;
	}
	public Server getServer() {
		return server;
	}
	public void setServer(Server server) {
		this.server = server;
	}
	public WebAppContext getContext() {
		return context;
	}
	public void setContext(WebAppContext context) {
		this.context = context;
	}
	
	public static Long getTime(){
		return System.currentTimeMillis();
	}
	
	
}
